package com.latamcloud.socket.client;
/*  The java.net package contains the basics needed for network operations. */

import java.net.*;

/* The java.io package contains the basics needed for IO operations. */
import java.io.*;

public class SocketClient {
	public static void main(String[] args) {

		final char INIT_COMM = (char) 1;
		final char INIT_TEXT = (char) 2;
		final char SEPARADOR = (char) 9;
		final char END_COMM = (char) 13;
		
		/** Define a host server */
	    String host = "host.latamcloud.com";

	    if (args.length > 0){
	    	host = args[0];
	    }
	    
	    System.out.println("Host: " + host);
	    
	    /** Define a port */
	    int port = 19999;

	    StringBuffer instr = new StringBuffer();
	    String TimeStamp;
	    
	    System.out.println("SocketClient initialized");
	 
	    while (true) {
		    
	    	try 
		    {
	    		// INICIO LA CONEXION
		        InetAddress address = InetAddress.getByName(host);
		        Socket connection = new Socket(address, port);
		        BufferedOutputStream bos = new BufferedOutputStream( connection.getOutputStream() );
		        //
		        
		        // PREPARO EL MENSAJE A ENVIAR
		        OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
	            TimeStamp = new java.util.Date().toString();

	            String messageToSend =  "";
	            
	            if (args.length > 1)   {
	            	String aux = "";
	            	
	            	for (int i = 1; i < args.length; i++) {
	            		aux = aux + args[i].toString() + " ";
					}
	            	messageToSend = INIT_COMM + aux + END_COMM;
	            } else {
	            	messageToSend = 
	            		INIT_COMM +
	            		"campo 1 " + SEPARADOR + " campo 2 " + SEPARADOR + TimeStamp + SEPARADOR 
	            		+  END_COMM;
	            }
	            
	            // char(13) fin de mensaje

	            
	            osw.write(messageToSend);
	            osw.flush();
	            
	            
	            /** Instantiate a BufferedInputStream object for reading
	            /** Instantiate a BufferedInputStream object for reading
	             * incoming socket streams.
	             */
	
	            BufferedInputStream bis = new BufferedInputStream(connection.
	                getInputStream());
	            
	            
	            InputStreamReader messageReceived = new InputStreamReader(bis, "US-ASCII");
	
	            int c;
	            
	            while ( (c = messageReceived.read()) != 13)
	              instr.append( (char) c);
	
	            /** Close the socket connection. */
	            connection.close();
	            System.out.println(instr);
	            instr.delete(0, instr.length());
		    }
	    	catch (IOException f) 
          	{
          		System.out.println("IOException: " + f);
          	}
	    	catch (Exception g) 
	    	{
	            System.out.println("Exception: " + g);
	    	}
	    	
			try {
				Thread.sleep(1000);
			}
			catch (Exception e){}
	    }
          
    }
}